package com.springtrans.controller;

import com.springtrans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.annotation.RequestScope;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;
    @Autowired
    private UserService userService;
    @RequestMapping("/registry")
    public String registry(String name,String password){
        TransactionStatus status=dataSourceTransactionManager.getTransaction(transactionDefinition);
        userService.registryUser(name,password);
        //提交事务
        dataSourceTransactionManager.commit(status);
        //回滚事务
//        dataSourceTransactionManager.rollback(status);
        return "z注册成功";
    }

    @Transactional
    @RequestMapping("/registry1")
    public String registry1(String name,String password){
        userService.registryUser(name,password);
        try {
            int a=10/0;
        }catch (Exception e){
            e.printStackTrace();
        }

        return "用户注册成功";
    }



}
